package com.jh.paymentchannel.repository.freeQuick;


import com.jh.paymentchannel.pojo.freeQuick.FreeQuickWithdraw;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface FreeQuickWithdrawRepository extends JpaRepository<FreeQuickWithdraw, String>,
        JpaSpecificationExecutor<FreeQuickWithdraw> {

    @Query("select f from FreeQuickWithdraw f where f.requestNo =:requestNo and f.merchantNo = :merchantNo")
    FreeQuickWithdraw findFreeQuickWithdrawPageByMerchantNoAndRAndRequestNo(@Param("requestNo")String requestNo, @Param("merchantNo")String merchantNo);

    @Query("select sum(f.gradeAmount) from FreeQuickWithdraw f where f.userId = :userId and f.payStatus = '1' and date_format(f.createTime,'%Y-%m-%d') >= :formatDate")
    BigDecimal getTodayIncome(@Param("userId") String userId, @Param("formatDate") String formatDate);

    /**
     * 查询待提现的任务
     * @return
     */
    @Query("select f from FreeQuickWithdraw f where f.payStatus = '0'")
    List<FreeQuickWithdraw> getAllInProcess();


    @Query("select f from FreeQuickWithdraw f where f.payStatus in (0,3,4)")
    List<FreeQuickWithdraw> getAllInProcessPay();

    @Modifying
    @Query("update FreeQuickWithdraw f set f.payStatus = :resultStatus where f.withdrawRequestNo = :withdrawRequestNo")
    int updateByRequestNo(@Param("resultStatus") String resultStatus, @Param("withdrawRequestNo") String withdrawRequestNo);

    @Query("select f from FreeQuickWithdraw f where f.userId = :userId and f.payStatus = :status and f.payType <> '3'")
    Page<FreeQuickWithdraw> rechargePage(@Param("userId") String userId, @Param("status") String status, Pageable pageable);

    @Query("select f from FreeQuickWithdraw f where f.userId = :userId and f.payType <> '3'")
    Page<FreeQuickWithdraw> rechargePageNotStatus(@Param("userId") String userId, Pageable pageable);

    @Query("select f from FreeQuickWithdraw f where f.merchantNo = :merchantNo and f.requestNo = :requestNo")
    FreeQuickWithdraw getByMerchantNoAndRequestNo(@Param("merchantNo") String merchantNo, @Param("requestNo") String requestNo);
}
